package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.apache.ibatis.session.SqlSession;


import java.time.LocalDate;
import java.util.List;

/**
 * @Title: productServiceImpl
 * @Package: com.project.service.impl
 * @Description:
 * @Author: Liang-Xie
 * @Date: 2022/7/12 - 17:26
 */
@SuppressWarnings("all")
public class productServiceImpl extends BaseService implements IProductService {
    @Override
    public void add(ProductBean productBean) {
//        创建并得到会话对象
        SqlSession session=this.getSession();
//        创建并返回mapper接口对象
        IProductMapper mapper=session.getMapper(IProductMapper.class);
        mapper.add(productBean);
//        提交事务
        session.commit();
//        关闭连接
        session.close();
    }

    @Override
    public int addProduct(ProductBean productBean) {
        SqlSession session=this.getSession();
        IProductMapper mapper=session.getMapper(IProductMapper.class);
        mapper.addProduct(productBean);
        session.commit();
        session.close();
        return 0;
    }

    @Override
    public void del(Integer id) {
    SqlSession session=this.getSession();
    IProductMapper mapper=session.getMapper(IProductMapper.class);
    mapper.del(id);
    session.commit();
    session.close();
    }

    @Override
    public void update(Integer id,Integer price) {
    SqlSession session=this.getSession();
    IProductMapper mapper=session.getMapper(IProductMapper.class);
    mapper.update(id,price);
    session.commit();
    session.close();
    }

    @Override
    public List<ProductBean> findAll() {
        SqlSession session=this.getSession();
        IProductMapper mapper=session.getMapper(IProductMapper.class);
        List<ProductBean>productBeanList= mapper.findAll();
        session.close();
        return productBeanList;
    }

    @Override
    public ProductBean findById(Integer id) {
        SqlSession session=this.getSession();
        IProductMapper mapper=session.getMapper(IProductMapper.class);
        ProductBean productBean=mapper.findById(id);
        session.close();
        return productBean;
    }

    @Override
    public List<ProductBean> findByName(String name) {
        SqlSession session=this.getSession();
        IProductMapper mapper=session.getMapper(IProductMapper.class);
        List<ProductBean>productBeanList=mapper.findByName(name);
        session.close();
        return productBeanList;
    }

    @Override
    public List<ProductBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        SqlSession session=this.getSession();
        IProductMapper mapper=session.getMapper(IProductMapper.class);
        List<ProductBean>productBeanList=mapper.findByItem(name, startDate, endDate);
        session.close();
        return productBeanList;
    }

    @Override
    public CutPageBean<ProductBean> cutByItem(int pageNO, String name, LocalDate startDate, LocalDate endDate) {
        SqlSession session=this.getSession();
        IProductMapper mapper=session.getMapper(IProductMapper.class);
        CutPageBean<ProductBean>cutPageBean=new CutPageBean<>();
        cutPageBean.setList(mapper.cutList(name,startDate,endDate,(pageNO-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE));
        cutPageBean.setCount(mapper.cutCount(name, startDate, endDate));
        return cutPageBean;
    }

    @Override
    public void addMore(List<ProductBean> productBeanList) {
        SqlSession session=this.getSession();
        IProductMapper mapper=session.getMapper(IProductMapper.class);
        mapper.addMore(productBeanList);
        session.commit();
        session.close();
    }

    @Override
    public void delMore(int[] idArray) {
        SqlSession session=this.getSession();
        IProductMapper mapper=session.getMapper(IProductMapper.class);
        mapper.delMore(idArray);
        session.commit();
        session.close();
    }

    @Override
    public List<ProductBean> findByPrice(Integer lowPrice, Integer highPrice) {
        SqlSession session=this.getSession();
        IProductMapper mapper=session.getMapper(IProductMapper.class);
        List<ProductBean>productBeanList=mapper.findByPrice(lowPrice,highPrice);
        session.commit();
        session.close();
        return productBeanList;
    }


}

